package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.UserService;
import com.sun.javafx.logging.PulseLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    @PostMapping
    public R addAddress(@RequestBody AddressBook addressBook){
        //获取userId

        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);

        return R.success("保存地址成功");
    }

    @PutMapping("/default")
    public R defaultAddress(@RequestBody AddressBook addressBook){

        //先将该用户所有地址的is_default更新为0
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        luw.set(AddressBook::getIsDefault,0);
        luw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        addressBookService.update(luw);

        //设置默认值
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success("设置成功");
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){

        AddressBook address = addressBookService.getById(id);
        if(address != null){
            return R.success(address);
        }
        return R.error("查询失败");
    }

    @GetMapping("/default")
    public R getDefault(){

        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook address = addressBookService.getOne(lqw);

        if (address != null) {
            return R.success(address);
        }
        return R.error("查询失败");
    }

    @GetMapping("/list")
    public R getByUserId(){

        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(lqw);

        return R.success(list);
    }

    @DeleteMapping
    public R deleteAddress(@RequestParam List<Long> ids){
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }
}
